package com.well.rpc.config;

import com.well.rpc.job.JobHandler;
import com.well.rpc.job.WellJobHandler;
import com.well.rpc.netty.ClientSsl;
import com.well.rpc.utils.ThreadPoolUtils;
import com.well.rpc.model.RegisterModel;
import com.well.rpc.model.TransData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
public class ClientDataConfig {

    /* save client start status */
    public static boolean isStart;

    private static TransData registerModel;

    /* save client register status */
    private static AtomicBoolean isRegister = new AtomicBoolean(false);

    /* save register server model*/
    private static List<RegisterModel> serverModels = Collections.synchronizedList(new ArrayList<>());

    /* save job handle. key is handler name */
    private static Map<String, WellJobHandler> jobHandlerMap = new HashMap<>();

    /* client channel ssl cert file */
    private static ClientSsl clientSsl = new ClientSsl();

    public ClientDataConfig() {
    }

    /**
     *  设置注册服务
     * @param serverModel serverModel
     */
    public static void setRegisterModels(List<RegisterModel> serverModel) {
        serverModel.clear();
        serverModels.addAll(serverModel);
    }

    /**
     * @param isRegister 是否注册
     */
    public static void setClientRegisterStatus(Boolean isRegister) {
        ClientDataConfig.isRegister.set(isRegister);
    }

    /**
     * @return 是否注册
     */
    public static Boolean getClientRegisterStatus() {
        return ClientDataConfig.isRegister.get();
    }
    /**
     *  获取注册的服务
     * @return registerModels
     */
    public static List<RegisterModel> getRegisterModels() {
        return serverModels;
    }

    /**
     * 清除注册服务数据
     */
    public static void clearRegisterModels() {
        if (!getClientRegisterStatus()) {
            log.warn(" Clear All Server Model !");
            serverModels.clear();
        }
    }

    /**
     * 获取传输数据
     * @return TransData
     */
    public static TransData getRegisterTransData() {
        return registerModel;
    }

    /**
     * 设置数据模型
     * @param modelData 传输数据
     */
    public static void setRegisterData(TransData modelData) {
        registerModel = modelData;
    }

    /**
     * 异步清清除注册数据
     */
    public static void clearRegisterModelsAsync() {
        ThreadPoolUtils.execute(ClientDataConfig::clearRegisterModels);
    }

    /**
     * 保存任务处理器
     * @param jobName 任务名
     * @param jobHandler 任务处理器
     */
    public static void saveJobHandler(String jobName, WellJobHandler jobHandler) {
        JobHandler.checkCronExpression(jobName, jobHandler.cronTrigger());
        jobHandlerMap.put(jobName, jobHandler);
    }

    /**
     * 获取任务处理器
     * @param jobName 任务名
     */
    public static WellJobHandler getJobHandler(String jobName) {
        return jobHandlerMap.get(jobName);
    }

    /**
     *  获取注册的所有定时任务
     * @return map
     */
    public static Map<String, WellJobHandler> getJobHandlerMap() {
        return jobHandlerMap;
    }

    /**
     *  设置通信ssl信息
     * @param enableSsl
     * @param certPath
     * @param keyPath
     * @param caPath
     */
    public static void setClientSsl(boolean enableSsl, String certPath, String keyPath, String caPath) {
        clientSsl.setSslCtx(enableSsl, certPath, keyPath, caPath);
    }

    public static ClientSsl getClientSsl() {
        return clientSsl;
    }
}
